ActiveAdmin.register_page "ChoiceDashboard" do
  menu priority: 10, label: '自选看板'

  content title: "" do
    redis_is_choices = Parallel.map(Stock.is_choice?, in_processes: 1, progress: false) do |stock|
      JSON.parse $Redis.get(stock.symbol)
    end
    choices = Stock.is_choice?
    # alerts = Stock.is_alert?.sort_by{|x| x.ths_flow }.reverse
    alerts = Stock.is_alert?.sort_by { |x| (x.ths_flow || 0).to_f }.reverse
    choice_upf3_0 = choices.select{|choice| redis_is_choices.find { |x| x["code"] == choice.symbol }["changepercent"].to_f > -1 && redis_is_choices.find { |x| x["code"] == choice.symbol }["changepercent"].to_f <= 0 }
    choice_up0_3 = choices.select{|choice| redis_is_choices.find { |x| x["code"] == choice.symbol }["changepercent"].to_f > 0 && redis_is_choices.find { |x| x["code"] == choice.symbol }["changepercent"].to_f <= 3 }
    choice_flow_in = choices.where("ths_flow > 0")

    columns do
      column do
        panel "alert" do
          table_for alerts do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].to_f.round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["trade"].to_f.round(1)
            end
            column :buy_price
            column :sell_price
            column :ma_alert
            column :note
            column :block do |stock|
              div do
                stock.blocks.pluck(:name).each do |sk|
                  span link_to "#{sk}", admin_block_path(Block.find_by_name(sk).id), target: '_blank', rel: "noopener noreferrer"
                end
              end
            end
            column do |sk|
              stock = Stock.find_by_ts_code(sk.ts_code)
              fluctuation = Fluctuation.find_by_code(stock.symbol)
              if fluctuation.present?
                fluctuation.title
              else
                "F:无"
              end
            end
          end
        end
      end
    end
    columns do
      column do
        panel "-3 < 自选 < 0 " do
          table_for choice_upf3_0.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].to_f.round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :buy_price
            column :sell_price
            column :ma_alert
            column :block do |stock|
              div do
                stock.blocks.pluck(:name).each do |sk|
                  span link_to "#{sk}", admin_block_path(Block.find_by_name(sk).id), target: '_blank', rel: "noopener noreferrer"
                end
              end
            end
            column :edit do |stock|
              link_to "编辑", edit_admin_stock_path(stock.id), class: "button"
            end
          end
        end
      end
      column do
        panel "-1 < 自选 < 3 " do
          table_for choice_up0_3.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].to_f.round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :buy_price
            column :sell_price
            column :ma_alert
            column :block do |stock|
              div do
                stock.blocks.pluck(:name).each do |sk|
                  span link_to "#{sk}", admin_block_path(Block.find_by_name(sk).id), target: '_blank', rel: "noopener noreferrer"
                end
              end
            end
            column :edit do |stock|
              link_to "编辑", edit_admin_stock_path(stock.id), class: "button"
            end
          end
        end
      end
    end
    columns do
      column do
        panel "资金流入" do
          table_for choice_flow_in.sort_by{|x|x.ths_flow}.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].to_f.round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :buy_price
            column :sell_price
            column :ma_alert
            column :block do |stock|
              div do
                stock.blocks.pluck(:name).each do |sk|
                  span link_to "#{sk}", admin_block_path(Block.find_by_name(sk).id), target: '_blank', rel: "noopener noreferrer"
                end
              end
            end
            column :edit do |stock|
              link_to "编辑", edit_admin_stock_path(stock.id), class: "button"
            end
          end
        end
      end
    end
    columns do
      column do
        panel "全部自选股票列表" do
          table_for choices.sort_by{|x|(JSON.parse $Redis.get(x.symbol))["changepercent"].to_f}.reverse do
            column '【name】' do |stock|
              code = %w(5 6 9).include?(stock.symbol[0]) ? "sh#{stock.symbol}" : "sz#{stock.symbol}"
              link_to "#{stock.name}", "https://finance.sina.com.cn/realstock/company/#{code}/nc.shtml", target: '_blank', rel: "noopener noreferrer"
            end
            column :symbol do |stock|
              link_to "#{stock.symbol}", admin_stock_path(stock.id)
            end
            column :ch do |stock|
              (JSON.parse $Redis.get(stock.symbol))["changepercent"].to_f.round(1)
            end
            column :yc do |stock|
              stock.year_count
            end
            column :vch do |stock|
              stock.volume_change
            end
            column :flow do |stock|
              link_to "#{stock.ths_flow}", "https://stockpage.10jqka.com.cn/#{stock.symbol}/funds/#funds_sszjlx", target: '_blank', rel: "noopener noreferrer"
            end
            column :buy_price
            column :sell_price
            column :ma_alert
            column :choice_block_tag
            column :block do |stock|
              div do
                stock.blocks.pluck(:name).each do |sk|
                  span link_to "#{sk}", admin_block_path(Block.find_by_name(sk).id), target: '_blank', rel: "noopener noreferrer"
                end
              end
            end
            column :edit do |stock|
              link_to "编辑", edit_admin_stock_path(stock.id), class: "button"
            end
            column do |sk|
              stock = Stock.find_by_ts_code(sk.ts_code)
              fluctuation = Fluctuation.find_by_code(stock.symbol)
              if fluctuation.present?
                fluctuation.title
              else
                "F:无"
              end
            end
          end
        end
      end
    end
  end
end